home *** CD-ROM | disk | FTP | other *** search
/ Clickx 75 / Clickx 75.iso / software / expressionweb / expressionwebv3 / ExpressionWeb_en.exe / Setup / WeNoLoc.cab / xweb.FPCLASS.dbquery.ascx < prev    next >
Encoding:
Text File  |  2009-06-09  |  15.9 KB  |  419 lines

  1. <%BeginASP%>@ Import Namespace="System.Collections" <%EndASP%>
  2. <%BeginASP%>@ Import Namespace="System.Collections.Specialized" <%EndASP%>
  3. <%BeginASP%>@ Import Namespace="System.Globalization" <%EndASP%>
  4. <%BeginASP%>@ Control ClassName="DBQuery" debug="true" Language="c#" <%EndASP%>
  5. <SCRIPT language="C#" Runat="server">
  6.         private System.Web.UI.Control _querySourceControl;
  7.         private string _querySourceString;
  8.         private bool _submitPressed = false;
  9.         private bool _resetPressed = false;
  10.         private bool _showDebug = false;
  11.  
  12.         public string QuerySource
  13.         {
  14.             get{
  15.                 return _querySourceString;
  16.             }
  17.             set{
  18.                 _querySourceString =value;
  19.             }
  20.         }
  21.  
  22.         public bool Submitted
  23.         {
  24.             get {
  25.                 return _submitPressed;
  26.             }
  27.         }
  28.  
  29.         protected override void OnInit(EventArgs e)
  30.         {
  31.             _querySourceControl = Page.FindControl(QuerySource);
  32.         }
  33.     
  34.         static public string GetUserField(DBQuery query, string fieldName)
  35.         {
  36.             if( query != null )
  37.             {
  38.                 System.Web.UI.Control theControl = query._querySourceControl.FindControl( fieldName );
  39.                 if( theControl != null && theControl is TextBox )
  40.                     return query.TextBoxField( (TextBox)theControl ).Replace( "'", "''" );
  41.             }
  42.  
  43.         if(HttpContext.Current.Request.QueryString[fieldName] != null)
  44.                 return HttpContext.Current.Request.QueryString[fieldName];
  45.             
  46.             if(HttpContext.Current.Request.Form[fieldName] != null)
  47.                 return HttpContext.Current.Request.Form[fieldName];
  48.  
  49.             return string.Empty;
  50.         }
  51.     
  52.         public string TextBoxField( TextBox theBox )
  53.         {
  54.             if( _resetPressed )
  55.             {
  56.                 theBox.Text = string.Empty;
  57.                 return string.Empty;
  58.             }
  59.         
  60.             string Result = theBox.Text;
  61.             if( Result.Length == 0 && theBox.Attributes["Default"] != null )
  62.                 Result = theBox.Attributes["Default"];
  63.         
  64.             return Result;
  65.         }
  66.  
  67.         public void ClickSubmitButton(Object s, EventArgs e)
  68.         {
  69.             _submitPressed = true;
  70.         }
  71.     
  72.         public void ClickResetButton(Object s, EventArgs e)
  73.         {
  74.             _submitPressed = true;
  75.             _resetPressed = true;
  76.         }
  77.     
  78.     
  79.         /* Params:
  80.                                                                                                                        
  81.         /* whereClause - the text of the query after the word "Where" that has values from the search form inserted into it
  82.          */
  83.         static public string CompleteWhereClause( DBQuery query, string whereClause )
  84.         {
  85.             // This method parses the 'WHERE' clause of a FPDB:DBRegion tag. It replaces 
  86.             // ::FieldName:: terms with information from user fields, and removes 
  87.             // 'ColumnName op ::FieldName::' phrases if the field name is not found or
  88.             // there is no value entered and no default.
  89.             //
  90.             // This method does *not* do any other syntax checking. If there is an error
  91.             // in the syntax of the clause, it will fall through to the DataView and an
  92.             // exception will be thrown from there.
  93.         
  94.             string    Result = string.Empty;
  95.  
  96.             string    FieldName    = string.Empty,
  97.                 Token        = string.Empty,
  98.                 Phrase        = string.Empty,
  99.                 Conjunction    = string.Empty;
  100.                 
  101.             int        StartIndex = 0,
  102.                 EndIndex = 0;
  103.             bool    InField = false,
  104.                 ValidPhrase = true,
  105.                 FirstPhrase = true;
  106.                 
  107.             string    DebugString        = string.Empty;
  108.         
  109.             while( EndIndex < whereClause.Length )
  110.             {
  111.                 // Get a "term", either a string of letters and digits or some other single character
  112.                 if( !System.Char.IsLetterOrDigit( whereClause, EndIndex ) )
  113.                     EndIndex++;
  114.                 else
  115.                     while( System.Char.IsLetterOrDigit( whereClause, EndIndex ) && ++EndIndex < whereClause.Length );
  116.             
  117.                 Token = whereClause.Substring( StartIndex, EndIndex - StartIndex );
  118.             
  119.                 if( InField )
  120.                 {
  121.                     // We're parsing a field name ...
  122.                     if( Token == ":" && whereClause[ StartIndex + 1 ] == ':' )
  123.                     {
  124.                         // ... and we've reached the end ...
  125.                         EndIndex = StartIndex += 2;
  126.                         InField = false;
  127.                         if(!System.Char.IsLetter(FieldName,0))
  128.                         {
  129.                             FieldName = "Field" + FieldName;
  130.                         }
  131.                         if( GetUserField( query, FieldName ).Length != 0 )
  132.                         {
  133.                             Phrase += GetUserField( query, FieldName ).Replace( "'", "''" );
  134.                         }
  135.                         else 
  136.                         {
  137.                             // ... but it doesn't exist or the user didn't enter anything,
  138.                             ValidPhrase = false;
  139.                         }
  140.                     }                    
  141.                     else
  142.                     {
  143.                         // ... and we continue to parse the field name
  144.                         FieldName += Token;
  145.                         StartIndex = EndIndex;
  146.                     }
  147.                 }
  148.                 else
  149.                 {
  150.                     DebugString += Token + ";";
  151.                     if( Token == ":" && whereClause[ StartIndex + 1 ] == ':' )
  152.                     {
  153.                         // This is the beginning of a field
  154.                         EndIndex = StartIndex += 2;
  155.                         InField = true;
  156.                         FieldName = string.Empty;
  157.                     }
  158.                     else if( Token.Equals("(") )
  159.                     {
  160.                         // An open paren gets passed on to the result
  161.                         Result += Token;
  162.                         StartIndex = EndIndex;
  163.                     }
  164.                     else if( Token.Equals(")") )
  165.                     {
  166.                         // A close paren signals the end of a phrase
  167.                     
  168.                         if( ValidPhrase )
  169.                         {
  170.                             if( !FirstPhrase )
  171.                             {
  172.                                 Result += Conjunction;
  173.                             }
  174.                             Result += Phrase;
  175.                             Conjunction = Token;
  176.                             FirstPhrase = false;
  177.                         }
  178.  
  179.                         // But it might be empty, so read backward through the result and find out
  180.                         // if there's anything between this paren and its match
  181.                         int nIndex = Result.Length;
  182.                         while( System.Char.IsWhiteSpace( Result, --nIndex ) )
  183.                             if( nIndex == 0 )
  184.                             {
  185.                                 throw new ApplicationException( "<%IDS_DBREGION_ASPNET_ERROR_PARAMETER_COLONCOLON_DELIM%>" );
  186.                             }
  187.                             
  188.                         if( Result[ nIndex ] == '(' )
  189.                         {
  190.                             Result = ( nIndex > 0 ) ? "" : Result.Substring( 0, nIndex );
  191.                         }
  192.                         else
  193.                         {
  194.                             Result += ")";                        
  195.                             Conjunction = string.Empty;
  196.                             Phrase = string.Empty;
  197.                             ValidPhrase = true;
  198.                         }
  199.                         StartIndex = EndIndex;
  200.                     }
  201.                     else if( Token.ToLower(CultureInfo.InvariantCulture ).Equals( "or" ) || Token.ToLower(CultureInfo.InvariantCulture).Equals( "and" ) )
  202.                     {
  203.                         // This is a conjunction between phrases
  204.                         if( ValidPhrase )
  205.                         {
  206.                             if( !FirstPhrase )
  207.                                 Result += Conjunction;
  208.                             Result += Phrase;
  209.                             Conjunction = Token;
  210.                             FirstPhrase = false;
  211.                         }
  212.                         else if( !Conjunction.ToLower(CultureInfo.InvariantCulture).Equals( "or" ) )
  213.                             // The last phrase wasn't included by the user. We'll preserve "or" and move on.
  214.                             Conjunction = Token;
  215.         
  216.                         Phrase = string.Empty;
  217.                         StartIndex = EndIndex;
  218.                         ValidPhrase = true;
  219.                     }
  220.                     else
  221.                     {
  222.                         // This is just part of a phrase
  223.                         Phrase += Token;
  224.                         StartIndex = EndIndex;
  225.                     }
  226.                 }
  227.             }
  228.  
  229.             if( InField )
  230.             {
  231.                 throw new ApplicationException( "<%IDS_DBREGION_ASPNET_ERROR_PARAMETER_COLONCOLON_DELIM%>" );
  232.             }
  233.             if( ValidPhrase )
  234.             {
  235.                 Result += Conjunction + Phrase;
  236.             }
  237.             if( query != null && query._showDebug)
  238.             {
  239.                 query.debugOutputLabel.Text = Result;
  240.             }
  241.  
  242.             return Result;
  243.         }
  244.         
  245.         public static String CompleteCustomSql(String sqlText, DBQuery query, ArrayList parameterList)
  246.         {
  247.             int curChar = 0;
  248.             int charMax = 0;
  249.             int colonStart = 0;
  250.             int SQuoteStart = 0;
  251.             int DQuoteStart = 0;   
  252.             int Start = 0;
  253.             int End = 0;
  254.  
  255.             parameterList.Clear();
  256.         
  257.             while ((((curChar + 5) < sqlText.Length) && sqlText.IndexOf("::", curChar) >= 0))
  258.             {
  259.                 charMax = sqlText.Length;
  260.                 colonStart = sqlText.IndexOf("::", curChar);
  261.                 SQuoteStart = sqlText.IndexOf('\'', curChar);
  262.                 DQuoteStart = sqlText.IndexOf('"', curChar);
  263.     
  264.                 if (SQuoteStart == -1) 
  265.                 {
  266.                     SQuoteStart = charMax;
  267.                 }
  268.     
  269.                 if (DQuoteStart == -1) 
  270.                 {
  271.                     DQuoteStart = charMax;
  272.                 }
  273.     
  274.     
  275.                 char QuoteDelim = '\0';
  276.                 int QuoteStart = -1;
  277.                 int QuoteEnd = charMax;
  278.                 bool fp_bQuoteFound = false;
  279.                 int PotQuoteEnd = 0;
  280.     
  281.                 if (colonStart > SQuoteStart && DQuoteStart > SQuoteStart)  //single quote is first sought for character
  282.                 {
  283.                     QuoteDelim = '\'';
  284.                     QuoteStart = SQuoteStart;
  285.                 }
  286.                 else if (colonStart > DQuoteStart && SQuoteStart > DQuoteStart)  //double quote is first sought for character
  287.                 {
  288.                     QuoteDelim = '"';
  289.                     QuoteStart = DQuoteStart;
  290.                 }
  291.                 else
  292.                 {
  293.                     QuoteStart = colonStart;
  294.                     //The :: comes before any ' or "
  295.                 }
  296.  
  297.                 if(QuoteDelim != '\0') 
  298.                 {
  299.                     PotQuoteEnd = QuoteStart + 1;
  300.                     while (fp_bQuoteFound == false && PotQuoteEnd < (charMax - 1))
  301.                     {
  302.                   
  303.                         PotQuoteEnd = sqlText.IndexOf(QuoteDelim, PotQuoteEnd);
  304.                              
  305.                         if(PotQuoteEnd == -1)
  306.                         {
  307.                             break;
  308.                         }
  309.  
  310.                         if(PotQuoteEnd == (charMax - 1))
  311.                         {
  312.                             QuoteEnd = PotQuoteEnd;
  313.                             fp_bQuoteFound = true;
  314.                             break;
  315.                         }
  316.             
  317.                         if(!sqlText.Substring( PotQuoteEnd + 1, 1).Equals(QuoteDelim))
  318.                         {
  319.                             QuoteEnd = PotQuoteEnd;
  320.                             fp_bQuoteFound = true;
  321.                         }
  322.                         else
  323.                         {
  324.                             PotQuoteEnd = PotQuoteEnd + 2;
  325.                         }
  326.                     }
  327.  
  328.                           
  329.                     if(fp_bQuoteFound == false)
  330.                     { 
  331.                     
  332.                         throw new ApplicationException("<%IDS_DBREGION_ASPNET_ERROR_NO_MATCH_QUOTE%>");
  333.                     }
  334.  
  335.                     if(colonStart > QuoteEnd)  //there is no user input in this literal string
  336.                     {
  337.                         curChar = QuoteEnd + 1;
  338.                         continue;
  339.                     }
  340.  
  341.                 }
  342.  
  343.                 Start = colonStart;
  344.                 // found a opening ::, find the close ::
  345.                 End = sqlText.IndexOf("::", Start + 2);
  346.     
  347.                 
  348.                 if (End == -1)
  349.                 {
  350.                     throw new ApplicationException( "<%IDS_DBREGION_ASPNET_ERROR_COLON_COLON_CUSTOM%>");
  351.                 }
  352.                 
  353.                 if (!fp_bQuoteFound)
  354.                 {
  355.                     QuoteEnd = End + 1;
  356.                 }
  357.  
  358.                 String Field = sqlText.Substring( Start + 2, End - (Start + 2));
  359.                 String Value = GetUserField(query, Field);
  360.  
  361.                 
  362.                 if (fp_bQuoteFound) 
  363.                 {
  364.                        
  365.                     String Lead = sqlText.Substring( QuoteStart + 1, colonStart - QuoteStart -1);
  366.                     String Tail = sqlText.Substring( End + 2, QuoteEnd - End - 2);
  367.                     if (QuoteDelim ==  '\"')  
  368.                     {
  369.                         Lead = Lead.Replace("\"\"", "\"");
  370.                         Tail = Tail.Replace("\"\"", "\"");
  371.                     }
  372.                     else  if (QuoteDelim == '\'' )
  373.                     {
  374.                         Lead = Lead.Replace("''", "'");
  375.                         Tail = Tail.Replace("''", "'");
  376.                     }
  377.  
  378.                     Value = Lead + Value + Tail;
  379.                 }
  380.  
  381.                 if(fp_bQuoteFound == false)
  382.                 {
  383.                     if(Value.Length == 0)
  384.                     {
  385.                         Value = "0";
  386.                     }
  387.                     try{
  388.                     parameterList.Add( Convert.ToDecimal(Value) );
  389.                     }
  390.                     catch( FormatException )
  391.                     {
  392.                         parameterList.Add( Convert.ToDecimal(0));
  393.                     }
  394.                 }
  395.                 else
  396.                 {
  397.                     parameterList.Add( Value );
  398.                 }
  399.                         
  400.                 if((sqlText.Length - QuoteEnd) == 1 )
  401.                 {
  402.                     sqlText = sqlText.Substring(0, QuoteStart) + "?";
  403.                 }
  404.                 else
  405.                 {
  406.                     sqlText = sqlText.Substring(0, QuoteStart) + "?" + sqlText.Substring( QuoteEnd + 1);
  407.                 }
  408.                  
  409.                 // Fixup the new current position to be after the substituted value
  410.                 curChar = QuoteStart + 1;
  411.             }
  412.             
  413.             return sqlText;
  414.         }
  415. </SCRIPT>
  416. <asp:Button ID="SubmitButton" Text="Submit" OnClick="ClickSubmitButton" RunAt="server" />
  417. <asp:Button ID="ResetButton" Text="Reset" OnClick="ClickResetButton" RunAt="server" />
  418. <asp:Label ID="debugOutputLabel" Runat="server" />
  419.